<template>
  <div>
    <div>
        <search-table :url="url"
                      ref="searchtable"
                      :hideSelect="true"
                      :columns="columns"
                      :form="form"
                      style="margin-top:20px;">

        <template slot="searchForm">
          <a-form layout="inline"
                  :form="form">
            <a-form-item label="赠品名称">
              <a-input v-decorator="['goodsName']"
                       placeholder="请输入赠品名称">
              </a-input>
            </a-form-item>

            <a-form-item label="赠品类型">
              <a-select style="width: 180px;" v-decorator="['goodsType']"
                        :options="goodsTypeList"
                        placeholder="请输入赠品类型">
              </a-select>
            </a-form-item>

            <a-form-item>
              <a-button type="primary" class="searchBtn" @click="searchData">搜索</a-button>
            </a-form-item>

            <a-form-item>
              <a-button type="primary" class="searchBtn" @click="resertSearchData">重置</a-button>
            </a-form-item>

            <a-form-item label="">
              <a-button @click="addGoods" type="primary"> 添加赠品</a-button>
            </a-form-item>

          </a-form>
        </template>


        <template slot="tableOperation" slot-scope="{ record }">
          <!-- <permission authority="">
                <span class="text-primary pointer" @click="openInfo(record,'dataVisible')">数据</span>
               </permission>
               <a-divider type="vertical"></a-divider>
           -->

          <permission authority="">
            <span class="text-primary pointer" @click="editGoods(record)">编辑</span>
          </permission>
          <a-divider type="vertical"></a-divider>

          <permission authority="">
            <span class="text-primary pointer" @click="deletes(record)">删除</span>
          </permission>

        </template>

      </search-table>

    </div>
  </div>
</template>

<script>
  import SearchTable from "./components/SearchTable";
  export default {
    name: "giveaway",
    components: {
      SearchTable
    },
    created() {

    },
    data() {
      return {
    /*    pagination: {
          showQuickJumper: true,
          total: 0,
          current: null,
          pageSize: 10,
          showTotal: (total) => `共${ total }条`,
          showSizeChanger: true,
          pageSizeOptions: ['10', '20', '50', '100', '200']
        },*/
        //类型数据
        status: "1",
        goodsTypeList:[{label:'全部',value:''},{label:'商城',value:'1'},{label:"付费会员",value: "2"},{label:"自建",value: "3"}],
        // url: 'goldenhome/member/admin_/benefits/card/page',
        url: 'goldenhome/gh/lottery_/admin/giving/goodsmanage/page',
        form: this.$form.createForm(this, {name: 'table_search'}),
        columns: [
          {
            title: '商品名称',
            dataIndex: 'goodsName',
            key: 'goodsName',
            align: 'center',
            customRender:(text,row)=>{
              return <div class="product_td"><table-img src={row.goodsPicture}/><div class="product_name">{text}</div></div>
            }
          },
          {
            title: '商品状态',
            dataIndex: 'status',
            key: 'status',
            align: 'center',
            customRender:(text,row)=>{
              if (row.goodsType==1&&text==0){
                return "下架"
              }
              if (row.goodsType==1&&text==1){
                return "上架"
              }
              if (row.goodsType==1&&text==-1){
                  return "仓库中"
              }
              if (row.goodsType==2&&text==1){
                return "已启用"
              }
              if (row.goodsType==2&&text==0){
                return "未启用"
              }
              if (row.goodsType==3){
                return "——"
              }
            }
          },
          {
            title: '商品来源',
            dataIndex: 'goodsType',
            key: 'goodsType',
            customRender:(text, row)=>{
              if (text ==1){
                return "商城"
              }else if (text==2){
                return "付费会员"
              }else if (text==3){
                return "自建"
              }else{
                return text
              }
            }
          },
          {
            title: '商品实际库存数',
            dataIndex: 'goodsStock',
            key: 'goodsStock',
            customRender:(text,row)=>{
              if (row.goodsType ==2){
                return "不限量"
              }else{
                return text
              }
            }
          },
          {
            title: '赠品库存数',
            dataIndex: 'stock',
            key: 'stock',
            align: 'center'
          },
          {
            title: '赠品剩余库存数',
            dataIndex: 'remainingStock',
            key: 'remainingStock',
            align: 'center',
          },
          {
            title: '赠品已被领取',
            dataIndex: 'prizePoolStock',
            key: 'prizePoolStock',
          },
          {
            title: '赠品已被使用',
            dataIndex: 'used',
            key: 'used',
          },
          {
            title: '操作',
            dataIndex: 'operation',
            scopedSlots: {customRender: 'operation'}
          }
        ],

      }
    },
    methods: {
      searchData(){
        this.$refs.searchtable.handleSearch()
      },

      resertSearchData(){
        console.log("重置搜索条件")
        this.form.resetFields()
        this.searchData()
        /*   this.form.setFieldsValue({
             cardName:'',
             status: "1"
           })*/
      },

      editGoods(record){
        // 跳到修改赠品页面
        console.log(8)
         let query =  {
                    id: record.id,
                    goodsId: record.goodsId,
                    goodsName: record.goodsName,
                    goodsPicture: record.goodsPicture,
                    goodsType: record.goodsType,
            // goodsSpecId: record.goodsSpecId,
            // norm: record.norm,
            // current:this.pagination.current
          }
        this.$router.push({
          name: 'MarketingGiveawayPagesEditGoods',
          query
        })
      },

      addGoods() {
        // 跳到添加商品页面
        this.$router.push({
            name: 'MarketingGiveawayPagesAddGoods'
          })
      },

      deletes(record){
        // this.$set(this,"name","wf");  这是不允许的
        console.log("删除赠品",record)
        this.$confirm({
          title: '显示提示',
          content: `赠品库中删除该商品吗?`,
          okText: '确认',
          cancelText: '取消',
          onOk: ()=> {
            this.$post('goldenhome/gh/lottery_/admin/giving/goodsmanage/delete',{id:record.id}).then(res => {
                if (res && res.returnCode === 1000) {
                  this.$message.success('操作成功!')
                  this.$refs.searchtable.refreshTable()
                }
            })
          }
        })
      }
    },
  }

</script>

<style scoped lang="less">
  .product_td{
    display: flex;
    align-items: center;
  .product_name{
    margin-left:6px;
  }
  }
</style>